﻿using System;
using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using NUnit.Framework;

namespace WindsorInitConfig {
    [TestFixture]
    public class IoC_168 {
        [Test]
        public void tt() {
            var container = new WindsorContainer(new XmlInterpreter(new StaticContentResource(@"<castle> 
    <components> 
        <component id='objectContextFactory'
                   service='WindsorInitConfig.IoC_168+IObjectContextFactory, WindsorInitConfig' 
                   type='WindsorInitConfig.IoC_168+ObjectContextFactory, WindsorInitConfig'> 
        </component> 
        <component id='userRepository'
                   service='WindsorInitConfig.IoC_168+IUserRepository, WindsorInitConfig' 
                   type='WindsorInitConfig.IoC_168+UserRepository, WindsorInitConfig'> 
            <parameters>
              <factory>${objectContextFactory}</factory>
            </parameters> 
        </component> 
    </components> 
</castle> ")));

            container.Resolve<IUserRepository>();
        }

        public interface IUserRepository {}

        public class UserRepository : IUserRepository {
            public UserRepository(IObjectContext objectContext) {}

            public UserRepository(IObjectContextFactory factory)
                : this(factory.CreateObjectContext()) {}
        }

        public interface IObjectContext {}

        public interface IObjectContextFactory {
            IObjectContext CreateObjectContext();
        }

        public class ObjectContextFactory: IObjectContextFactory {
            public IObjectContext CreateObjectContext() {
                return null;
            }
        }
    }
}